<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input allowClear placeholder="请输入商品名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button v-for="item of statusList" :key="item.id" :value="item.id"> {{ item.name }} </a-radio-button>
        </a-radio-group>

        <div>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('remove')">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :customRow="customRow"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          current: pageNo,
          pageSize,
          total,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="goodInfo" slot-scope="text, record">
          <div class="content" v-if="record.goods_info">
            <div class="img">
              <a-avatar :size="48" :src="record.goods_info.cover_pic" />
            </div>

            <div class="txt">
              <a-tooltip>
                <template slot="title"> {{ record.goods_info.name }} </template>
                <h3>{{ record.goods_info.name }}</h3>
              </a-tooltip>
              <p>{{ getAttrName(record.attr_list) }}</p>
            </div>
          </div>
          <span v-else>-</span>
        </span>

        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>

        <span slot="user" slot-scope="text, record">
          <span v-if="record.saasUser"> {{ record.saasUser.name }}</span>
          <span>-</span>
        </span>

        <span slot="total_price" slot-scope="text"> ¥ {{ text || '0.00' }} </span>
        <span slot="pay_price" slot-scope="text"> ¥{{ text || '0.00' }} </span>
        <span slot="activity" slot-scope="text"> ¥{{ text ? text.min_price : '0.00' }} </span>

        <span slot="status" slot-scope="text">
          <a-badge v-if="getStatus(text)" :color="getStatus(text).color" :text="getStatus(text).name" />
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="handleItem('remove', record)">删除</a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { orderList, orderDel } from '@/api/marketing/curPrice'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '商品信息',
    dataIndex: 'goodInfo',
    scopedSlots: {
      customRender: 'goodInfo',
    },
  },
  {
    title: '发起时间',
    dataIndex: 'created_at',
    scopedSlots: {
      customRender: 'created_at',
    },
  },
  {
    title: '发起人',
    scopedSlots: {
      customRender: 'user',
    },
  },
  {
    title: '原价',
    dataIndex: 'total_price',
    scopedSlots: {
      customRender: 'total_price',
    },
    sorter: (a, b) => parseInt(a.total_price || '0') - parseInt(b.total_price || '0'),
  },
  {
    title: '当前价',
    dataIndex: 'pay_price',
    scopedSlots: {
      customRender: 'pay_price',
    },
    sorter: (a, b) => parseInt(a.pay_price || '0') - parseInt(b.pay_price || '0'),
  },
  {
    title: '最低价',
    dataIndex: 'activity',
    scopedSlots: {
      customRender: 'activity',
    },
    sorter: (a, b) => parseInt(a.activity.min_price || '0') - parseInt(b.activity.min_price || '0'),
  },

  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 5,
      columns,
      queryParam: {
        name: '',
        status: '-1',
      },
      list: [],
      selectedRowKeys: [],
      statusList: [
        { id: '-1', name: '全部', color: '' },
        { id: '0', name: '进行中', color: '#65C817' },
        { id: '1', name: '已结束', color: '#918383' },
        { id: '2', name: '已失败', color: '#918383' },
        { id: '10', name: '待付款', color: '#ff0000' },
        { id: '11', name: '已付款', color: '#65C817' },
      ],
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getAttrName() {
      return (attr) => {
        if (attr && attr.length) {
          return attr.map((el) => el.attr_group_name + ':' + el.attr_name).join()
        }
        return ''
      }
    },

    getStatus() {
      return (status) => {
        let item = this.statusList.find((el) => el.id === String(status))

        return item
      }
    },
  },

  methods: {
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this
      const res = await orderList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.$nextTick(() => {
          this.selectedRowKeys = []
          let { totalCount, list: data } = res.data
          this.total = totalCount
          this.list = data
          this.pageNo = pageNo
        })
      }
      hide()
    },

    tablePageChange(e) {
      this.getList(e)
    },

    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    handleAll(type) {
      let { selectedRowKeys } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      let ids = selectedRowKeys.join(',')

      switch (type) {
        case 'remove':
          this.remove(ids)
          break
      }
    },

    handleItem(type, item) {
      switch (type) {
        case 'remove':
          this.remove(item.id)
          break
      }
    },

    remove(ids) {
      this.$confirm({
        title: '提示?',
        content: '是否删除？',
        onOk: () => {
          orderDel({
            ids,
          }).then((res) => {
            if (res.code) {
              this.$error({
                content: res.msg,
              })
            } else {
              this.$message.success(res.msg)
              this.getList()
              this.selectedRowKeys = []
            }
          })
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}
</style>
